<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width"/>
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>首页</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../css/main.css" />
    <link rel="stylesheet" type="text/css" href="../css/demo/job.css" />
</head>
<body>
  <div id="header">
      <h3>学习论坛</h3>
      <a class="myNotice" onclick="openPage('notice_list','forum/notice_list.html')"></a>
      <a class="issue" onclick="openPage('issue','forum/issue.html')"></a>
  </div>
  <div id="all">
  <div class="search">
    <input class="input" type="text" name="" placeholder="请输入您要搜索的关键字">
    <span class="msg_search">搜索</span>
  </div>
  <div class="notice" noticeType=''><img src="../image/notice-blue.png"/><span></span></div>
  <div class="bt">
  </div>
  <div class="bts">
  </div>
  <div class="search_content"></div>
  <!-- <div class="circle" onclick="openPage('chatContacts','project/chatContacts.html')" tapmode>
  	<img src="../image/say.png" class="book_img">
  </div> -->
  </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery.js"></script>
<script type="text/javascript">
  //---------------------------------------------------------
  //脚本函数

function reloadHtml(){

  window.location.reload();
  //selectType=selectInsex;
}
//---------------------------------------------------------
var flag=$api.getStorage('flag');
var jwt=$api.getStorage('jwt');
apiready=function(){
var items=[];
var msgUrl;
var selectType=0;
var count=0;
var noticeType=0;
var NVNavigationBar = api.require('NVNavigationBar');
getContent(selectType,count,5);
getSort();
getNotice(noticeType);
function getSort(){
    var Url='/type/types/student/1';
    var functionRet=function(ret1){
      var items=[{
            title: "论坛首页",
            bg: 'rgba(86, 119, 252,0.8)',
            alpha: 0.8,
            bgSelected: 'rgba(86, 119, 252,0.8)',
          }];
      for (var i = 0; i < ret1.data.length; i++) {
          items.push({
            title: ret1.data[i].name,
            bg: 'rgba(86, 119, 252,0.8)',
            alpha: 0.8,
            bgSelected: 'rgba(86, 119, 252,0.8)',
          });
      }
      NVNavigationBar.open({
          rect: {
               x: 0,
               y: 60,
               w: api.frameWidth,
               h: 44
          },
         styles: {
               orientation: 'horizontal',
               bg: '#EEEEEE',
               bgAlpha: 1,
               font: {
               size: 16,
               sizeSelected: 16,
               color: '#ccccff',
               colorSelected: '#FFFFFF',
               alpha: 1
               },
               itemSize: {
                   w: api.frameWidth/4,
                   h: 45
               }
         },
          items:items,
             selectedIndex: 0,
             fixedOn: api.frameName,
             fixed:true,
             id: 'first'
      }, function(ret, err) {
          if(ret.eventType=="click"){
            $(".bt").show();
            $(".search_content").html("");
             $('.input').val("");
            //msgUrl='/api/public-msgs?type='+ret.index+'&page=0&size=10';
            $(".bt").empty();
            count=0;
            getContent(ret.index,count,5);
            getNotice(ret.index);
            selectType=ret.index;
          }
      });
    }
    var functionErr=function(err1){
      return false;
    }
    getNew(Url,functionRet,functionErr);
}
//---------------------------------------------------------
// function myFunction(listModel){
//     api.openWin({
//               name: 'article',
//               url: 'forum/article.html',
//               pageParam: {
//                   id:$(this).attr('showModule-id')  //文章id
//               }
//           });
//     alert(1)
// }

//--------------------------------------------------------
//获取文章
function getContent(type,page,size){
  if(type==0){
    msgUrl='/api/public-msgs/top-list?page='+page+'&size='+size;
  }else{
    type--;
    msgUrl='/api/public-msgs/top-list?type='+type+'&page='+page+'&size='+size;
  }
  api.ajax({
    url: 'http://47.104.170.122/v1/portal/apigateway/universityfacade'+msgUrl+'&token='+jwt,
    method: 'GET',
    headers: {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Headers": "Authorization",
      "Content-Type" : 'application/json; charset=utf-8'
    }
  }, function(ret1, err) {
    if(ret1.length){
      for (var i = 0; i < ret1.length; i++) {
        if(ret1[i].title.length>6){
          ret1[i].title = ret1[i].title.substring(0,6)+"..."
        }
        //if(ret1[i].imageList!=""){
          var imageSrc=ret1[i].imageList.toString().split(',');
          var userHeaderImageUrl=ret1[i].userHeaderImageUrl;
          if(userHeaderImageUrl=="#"){
            userHeaderImageUrl='../image/personal_head.png'
          }
            $(".bt").append('<div class="showModule" showModule-id='+ret1[i].id+'><div class="headerImage"><img src='+userHeaderImageUrl+'><span>'+ret1[i].userName+'</span><span class="delete" style="color:red;" delete-id='+ret1[i].id+'>删除</span></div><div class="At" At-id='+ret1[i].id+'><h3 class="showModule_t">'+ret1[i].title+'</h3><span class="createTime">发表时间:'+ret1[i].createTime+'</span><div class="Image"></div><div class="article">'+ret1[i].content+'</div><div><img src='+"http://"+imageSrc[0].replace(/["'"]/g,"")+'></div></div></div>');
        //}
      if(ret1[i].topLevel==1){
        $(".showModule").eq(i).find(".headerImage").after('<div class="topArticle">此帖子已被版主置顶</div>');
      }
      if(flag!=7){
        $(".delete").css("display","none");
      }

      }
      //console.log("page:"+page)
      //console.log("count:"+count)
    }
    $(".delete").on("click",function(){
         deleteArticle($(this).attr('delete-id'));
     });
    /*
    上面代码是按依次显示出来
    下面是点击了上面排列的第几个，就获取那个的id跟内容传给下个页面
    */
    $(".At").on("click",function(){
        api.openWin({
            name: 'article',
            url: 'forum/article.html',
            pageParam: {
                id:$(this).attr('At-id')
            }
        });
    });
  });
}
//--------------------------------------------------------
//删除文章
function deleteArticle(id){
    var url="/api/public-msgs/"+id;
    var data="";
    var functionRet=function(ret1){
      //alert("删除成功！");
    }
    var functionErr=function(err1){
      if(err1.statusCode==200){
        alert("文章删除成功！");
        var jsfun = 'reloadHtml();';
        api.execScript({
            name: 'root',
            frameName: 'frame2',
            script: jsfun
        });
        window.location.reload();
      }
    }
    deleteData(url,data,functionRet,functionErr);
}
//---------------------------------------------------------------------------------------------
//每5条加载一次数据
 $("#all").scroll(function(){
 var viewH =$(this).height();//可见高度
 var contentH =$(this).get(0).scrollHeight;//内容高度
 var scrollTop =$(this).scrollTop();//滚动高度
 //if(contentH - viewH - scrollTop <= 100) { //到达底部100px时,加载新内容
 if(scrollTop/(contentH -viewH)>=1){ //到达底部5%以内,加载新内容
 // 这里加载数据..
 count++;
 getContent(selectType,count,5);
 }
 });

//通过关键字搜索帖子
$(".msg_search").click(function(){
  var keyword=$(".search input").val();
  var searchUrl='/api/public-msgs/search?sort=createTime,desc&key='+keyword;
  var functionRet=function(ret1){
      if(ret1.length){
        //清空搜索结果
        $(".search_content").html("");
        for (var i = 0; i < ret1.length; i++) {
            var imageSrc=ret1[i].imageList.toString().split(',')
              $(".bt").hide();
              $(".search_content").append('<div class="showModule" showModule-id='+ret1[i].id+'><div class="headerImage"><img src='+ret1[i].userHeaderImageUrl+'><span>'+ret1[i].userName+'</span><span class="delete" style="color:red;" delete-id='+ret1[i].id+'>删除</span></div><div class="At" At-id='+ret1[i].id+'><h3 class="showModule_t">'+ret1[i].title+'</h3><span class="createTime">发表时间:'+ret1[i].createTime+'</span><div class="Image"></div><div class="article">'+ret1[i].content+'</div><div><img src='+"http://"+imageSrc[0].replace(/["'"]/g,"")+'></div></div></div>');
        if(flag!=7){
          $(".delete").css("display","none");
        }

        }
      }else{
        alert("没有查找内容，请重新输入...")
      }
      /*
      上面代码是按依次显示出来
      下面是点击了上面排列的第几个，就获取那个的id跟内容传给下个页面

      */
      $(".delete").on("click",function(){
           deleteArticle($(this).attr('delete-id'));
      });
      /*
      上面代码是按依次显示出来
      下面是点击了上面排列的第几个，就获取那个的id跟内容传给下个页面
      */
      $(".At").on("click",function(){
          api.openWin({
              name: 'article',
              url: 'forum/article.html',
              pageParam: {
                  id:$(this).attr('At-id')
              }
          });
      });

      // $(".search_content").on("click",".showModule",function(){
      //     api.openWin({
      //         name: 'article',
      //         url: 'forum/article.html',
      //         pageParam: {
      //             id:$(this).attr('showModule-id')
      //         }
      //     });
      // });
    }
    var functionErr=function(err1){
      return false;
    }
    getU(searchUrl,functionRet,functionErr);
})
//
  if(flag==7){
    $(".myNotice").show();
  }

  //显示通知
  function getNotice(noticeType){
    var url='/api/annunciates?type='+noticeType;
    var functionRet=function(ret){
      if(ret.length){
        var notice = ret[ret.length-1].content;
        var noticeType = ret[ret.length-1].type;
        if(notice.length>36){
          notice = notice.substring(0,36)+"..."
        }
        $(".notice span").html(notice);
        $(".notice").attr('noticeType',noticeType);
      }
    }
    var functionErr=function(err){
      return false;
    }
    get(url,functionRet,functionErr);
  }

  //点击公告，打开公告详情页
  $(".notice").on("click",function(){
    // alert(1)
      var noticeType=$(this).attr("noticeType");
      var notice=$(this).find("span").html();
      // console.log(JSON.stringify(noticeId));
      // 打开一个新的页面，并且传递参数
      api.openWin({
          name: 'notice',
          url: 'forum/notice.html',
          pageParam: {
              noticeType: noticeType
          }
      });
  })
}
</script>
<script type="text/javascript" src="../script/main.js"></script>
</html>
